Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 772)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.04509 13.04069 13.03633 13.03203 13.02778 13.02357 13.01941 13.01529
## [9] 13.01122 13.00720 13.00321 12.99927 12.99537 12.99150 12.98768 12.98389
## [17] 12.98014 12.97643 12.97275 12.96910 12.96549 12.96191 12.95835 12.95483
## [25] 12.95134 12.94787 12.94443 12.94102 12.93763 12.93427 12.93092 12.92760
## [33] 12.92430 12.92102 12.91776 12.91451 12.91129 12.90807 12.90488 12.90169
## [41] 12.89852 12.89536 12.89221 12.88907 12.88592 12.88277 12.87962 12.87647
## [49] 12.87334 12.87022 12.86711 12.86403 12.86096 12.85793 12.85492 12.85194
## [57] 12.84899 12.84609 12.84323 12.84041 12.83764 12.83492 12.83225 12.82965
## [65] 12.82710 12.82462 12.82221 12.81987 12.81760 12.81541 12.81330 12.81127
## [73] 12.80933 12.80748 12.80572 12.80406 12.80250 12.80105 12.79970 12.79846
## [81] 12.79733 12.79632 12.79543 12.79466 12.79402 12.79339 12.79265 12.79183
## [89] 12.79091 12.78993 12.78887 12.78775 12.78658 12.78537 12.78413 12.78285
## [97] 12.78156 12.78027 12.77896 12.77767 12.77639 12.77514 12.77392 12.77274
## [105] 12.77161 12.77053 12.76952 12.76859 12.76774 12.76698 12.76632 12.76576
## [113] 12.76533 12.76501 12.76483 12.76480 12.76491 12.76518 12.76561 12.76622
## [121] 12.76702 12.76800 12.76919 12.77058 12.77219 12.77403 12.77609 12.77841
## [129] 12.78163 12.78634 12.79239 12.79964 12.80793 12.81713 12.82709 12.83766
## [137] 12.84871 12.86008 12.87163 12.88321 12.89468 12.90590 12.91672 12.92699
## [145] 12.93657 12.94531 12.95308 12.95972 12.96508 12.97104 12.97936 12.98980
## [153] 13.00211 13.01603 13.03132 13.04771 13.06497 13.08284 13.10107 13.11940
## [161] 13.13758 13.15537 13.17251 13.18875 13.20384 13.21752 13.22955 13.23968
## [169] 13.24765 13.25320 13.25773 13.26274 13.26818 13.27399 13.28012 13.28651
## [177] 13.29311 13.29986 13.30670 13.31358 13.32045 13.32724 13.33391 13.34040
## [185] 13.34665 13.35260 13.35821 13.36341 13.36815 13.37238 13.37604 13.37907
## [193] 13.38142 13.38304 13.38386 13.38383 13.38291 13.38102 13.37812 13.37415
## [201] 13.36905 13.36277 13.35526 13.34646 13.33550 13.32173 13.30543 13.28685
## [209] 13.26626 13.24394 13.22014 13.19513 13.16917 13.14255 13.11551 13.08833
## [217] 13.06127 13.03460 13.00858 12.98349 12.95958 12.93713 12.91640 12.89765
## [225] 12.87830 12.85578 12.83042 12.80257 12.77253 12.74066 12.70727 12.67269
## [233] 12.63726 12.60130 12.56515 12.52914 12.49359 12.45883 12.42520 12.39302
## [241] 12.36262 12.33434 12.30851 12.28544 12.26548 12.24751 12.23018 12.21345
## [249] 12.19729 12.18167 12.16656 12.15192 12.13772 12.12393 12.11052 12.09745
## [257] 12.08470 12.07222 12.05999 12.04798 12.03615 12.02447 12.01290 12.00142
## [265] 11.99000 11.97859 11.96717 11.95571 11.94527 11.93680 11.93012 11.92503
## [273] 11.92134 11.91886 11.91740 11.91677 11.91677 11.91723 11.91793 11.91870
## [281] 11.91934 11.91966 11.91947 11.91858 11.91679 11.91392 11.90978 11.90417
## [289] 11.89690 11.88885 11.88102 11.87338 11.86592 11.85860 11.85141 11.84432
## [297] 11.83730 11.83034 11.82341 11.81648 11.80954 11.80256 11.79551 11.78838
## [305] 11.78114 11.77376 11.76623 11.75851 11.75059 11.74244 11.73299 11.72134
## [313] 11.70774 11.69245 11.67572 11.65780 11.63894 11.61940 11.59943 11.57928
## [321] 11.55920 11.53945 11.52027 11.50193 11.48467 11.46874 11.45440 11.44190
## [329] 11.43149 11.42343 11.41796 11.41309 11.40678 11.39925 11.39070 11.38135
## [337] 11.37138 11.36103 11.35048 11.33996 11.32966 11.31980 11.31058 11.30221
## [345] 11.29490 11.28886 11.28429 11.28140 11.28040 11.28149 11.28489 11.29080
## [353] 11.29935 11.31042 11.32383 11.33943 11.35705 11.37651 11.39766 11.42033
## [361] 11.44435 11.46956 11.49578 11.52286 11.55062 11.57890 11.60753 11.63635
## [369] 11.66519 11.69388 11.72226 11.75016 11.77741 11.80385 11.82931 11.85363
## [377] 11.87663 11.89816 11.92086 11.94723 11.97687 12.00938 12.04435 12.08138
## [385] 12.12008 12.16003 12.20084 12.24210 12.28342 12.32438 12.36460 12.40366
## [393] 12.44117 12.47671 12.50990 12.54033 12.56759 12.59129 12.61102 12.62910
## [401] 12.64799 12.66756 12.68765 12.70812 12.72883 12.74963 12.77036 12.79089
## [409] 12.81106 12.83073 12.84975 12.86798 12.88527 12.90146 12.91643 12.93001
## [417] 12.94206 12.95243 12.96098 12.96756 12.97177 12.97342 12.97272 12.96986
## [425] 12.96502 12.95841 12.95021 12.94062 12.92983 12.91803 12.90543 12.89220
## [433] 12.87855 12.86467 12.85075 12.83698 12.82356 12.81068 12.79853 12.78731
## [441] 12.77721 12.76843 12.76115 12.75338 12.74319 12.73080 12.71647 12.70045
## [449] 12.68298 12.66432 12.64470 12.62439 12.60361 12.58263 12.56169 12.54104
## [457] 12.52093 12.50159 12.48329 12.46627 12.45077 12.43704 12.42534 12.41591
## [465] 12.40689 12.39638 12.38457 12.37164 12.35778 12.34315 12.32795 12.31236
## [473] 12.29656 12.28073 12.26505 12.24971 12.23488 12.22075 12.20751 12.19532
## [481] 12.18438 12.17487 12.16697 12.16086 12.15672 12.15413 12.15249 12.15174
## [489] 12.15183 12.15270 12.15428 12.15652 12.15935 12.16273 12.16657 12.17084
## [497] 12.17546 12.18038 12.18554 12.19088 12.19633 12.20185 12.20736 12.21281
## [505] 12.21814 12.22329 12.22965 12.23850 12.24963 12.26283 12.27789 12.29461
## [513] 12.31277 12.33216 12.35257 12.37380 12.39563 12.41785 12.44026 12.46264
## [521] 12.48479 12.50649 12.52753 12.54772 12.56682 12.58464 12.60097 12.61560
## [529] 12.62831 12.63889 12.64715 12.65286 12.65757 12.66290 12.66879 12.67514
## [537] 12.68187 12.68890 12.69615 12.70354 12.71098 12.71840 12.72571 12.73283
## [545] 12.73968 12.74618 12.75224 12.75779 12.76274 12.76701 12.77052 12.77319
## [553] 12.77494 12.77567 12.77532 12.77381 12.77104 12.76693 12.76142 12.75441
## [561] 12.74517 12.73320 12.71875 12.70208 12.68345 12.66311 12.64133 12.61835
## [569] 12.59444 12.56985 12.54483 12.51966 12.49457 12.46983 12.44570 12.42243
## [577] 12.40027 12.37950 12.36035 12.34310 12.32799 12.31231 12.29341 12.27163
## [585] 12.24733 12.22085 12.19254 12.16275 12.13183 12.10012 12.06797 12.03574
## [593] 12.00376 11.97240 11.94198 11.91288 11.88542 11.85997 11.83686 11.81645
## [601] 11.79909 11.78512 11.77246 11.75891 11.74461 11.72970 11.71434 11.69866
## [609] 11.68281 11.66694 11.65119 11.63570 11.62063 11.60611 11.59230 11.57934
## [617] 11.56736 11.55653 11.54698 11.53886 11.53231 11.52748 11.52452 11.52376
## [625] 11.52535 11.52908 11.53477 11.54225 11.55131 11.56178 11.57347 11.58620
## [633] 11.59977 11.61400 11.62871 11.64370 11.65880 11.67382 11.68857 11.70286
## [641] 11.71651 11.72934 11.74115 11.75177 11.76292 11.77631 11.79174 11.80899
## [649] 11.82783 11.84805 11.86943 11.89175 11.91481 11.93836 11.96221 11.98614
## [657] 12.00992 12.03333 12.05617 12.07821 12.09924 12.11903 12.13738 12.15405
## [665] 12.16885 12.18287 12.19735 12.21223 12.22745 12.24297 12.25871 12.27464
## [673] 12.29068 12.30680 12.32292 12.33900 12.35498 12.37080 12.38641 12.40176
## [681] 12.41678 12.43142 12.44563 12.45935 12.47253 12.48510 12.49702 12.50822
## [689] 12.51900 12.52968 12.54025 12.55069 12.56099 12.57115 12.58115 12.59097
## [697] 12.60062 12.61007 12.61931 12.62835 12.63715 12.64571 12.65403 12.66209
## [705] 12.66987 12.67737 12.68457 12.69147 12.69805 12.70437 12.71049 12.71641
## [713] 12.72214 12.72767 12.73301 12.73814 12.74307 12.74781 12.75234 12.75667
## [721] 12.76080 12.76472 12.76844 12.77196 12.77527 12.77837 12.78127 12.78397
## [729] 12.78645 12.78873 12.79080 12.79269 12.79439 12.79589 12.79719 12.79830
## [737] 12.79921 12.79992 12.80042 12.80073 12.80083 12.80073 12.80041 12.79989
## [745] 12.79916 12.79822 12.79706 12.79569 12.79411 12.79230 12.79028 12.78804
## [753] 12.78558 12.78291 12.78002 12.77691 12.77360 12.77007 12.76632 12.76237
## [761] 12.75821 12.75383 12.74925 12.74445 12.73945 12.73424 12.72883 12.72320
## [769] 12.71738 12.71134 12.70511 12.69867
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 772)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63009 12.62522 12.62045 12.61577 12.61118 12.60668 12.60227 12.59795
## [9] 12.59372 12.58958 12.58552 12.58155 12.57767 12.57388 12.57017 12.56654
## [17] 12.56300 12.55955 12.55618 12.55289 12.54968 12.54655 12.54351 12.54054
## [25] 12.53766 12.53486 12.53213 12.52948 12.52692 12.52443 12.52201 12.51967
## [33] 12.51741 12.51522 12.51311 12.51107 12.50911 12.50722 12.50540 12.50365
## [41] 12.50197 12.50037 12.49883 12.49737 12.49597 12.49464 12.49339 12.49221
## [49] 12.49110 12.49007 12.48912 12.48824 12.48745 12.48674 12.48611 12.48556
## [57] 12.48510 12.48473 12.48445 12.48426 12.48416 12.48415 12.48424 12.48442
## [65] 12.48470 12.48508 12.48556 12.48614 12.48682 12.48761 12.48851 12.48951
## [73] 12.49062 12.49184 12.49317 12.49461 12.49617 12.49785 12.49964 12.50155
## [81] 12.50358 12.50573 12.50801 12.51041 12.51293 12.51563 12.51855 12.52169
## [89] 12.52503 12.52856 12.53229 12.53620 12.54028 12.54453 12.54894 12.55350
## [97] 12.55820 12.56304 12.56801 12.57310 12.57831 12.58362 12.58903 12.59453
## [105] 12.60011 12.60577 12.61150 12.61728 12.62312 12.62901 12.63493 12.64089
## [113] 12.64686 12.65285 12.65885 12.66485 12.67083 12.67681 12.68276 12.68868
## [121] 12.69456 12.70039 12.70617 12.71189 12.71754 12.72312 12.72904 12.73570
## [129] 12.74302 12.75091 12.75931 12.76813 12.77729 12.78673 12.79635 12.80608
## [137] 12.81585 12.82558 12.83519 12.84460 12.85373 12.86251 12.87086 12.87870
## [145] 12.88596 12.89255 12.89840 12.90531 12.91495 12.92701 12.94123 12.95731
## [153] 12.97495 12.99388 13.01381 13.03445 13.05551 13.07671 13.09775 13.11835
## [161] 13.13823 13.15708 13.17464 13.19060 13.20469 13.21661 13.22607 13.23280
## [169] 13.23825 13.24403 13.25010 13.25640 13.26286 13.26944 13.27608 13.28272
## [177] 13.28930 13.29577 13.30208 13.30816 13.31396 13.31943 13.32450 13.32913
## [185] 13.33325 13.33680 13.33975 13.34201 13.34355 13.34430 13.34421 13.34321
## [193] 13.34127 13.33831 13.33428 13.32913 13.32280 13.31524 13.30535 13.29228
## [201] 13.27634 13.25781 13.23700 13.21419 13.18968 13.16377 13.13675 13.10892
## [209] 13.08057 13.05200 13.02350 12.99538 12.96791 12.94141 12.91616 12.89246
## [217] 12.87060 12.85089 12.83361 12.81623 12.79622 12.77384 12.74940 12.72316
## [225] 12.69541 12.66643 12.63651 12.60593 12.57496 12.54390 12.51302 12.48261
## [233] 12.45295 12.42432 12.39700 12.37128 12.34744 12.32575 12.30651 12.29000
## [241] 12.27532 12.26137 12.24810 12.23549 12.22350 12.21208 12.20119 12.19080
## [249] 12.18087 12.17136 12.16223 12.15344 12.14495 12.13673 12.12874 12.12094
## [257] 12.11328 12.10573 12.09826 12.09082 12.08337 12.07588 12.06830 12.06061
## [265] 12.05275 12.04469 12.03790 12.03367 12.03176 12.03191 12.03386 12.03736
## [273] 12.04214 12.04796 12.05456 12.06168 12.06905 12.07644 12.08358 12.09021
## [281] 12.09608 12.10093 12.10450 12.10654 12.10680 12.10501 12.10092 12.09571
## [289] 12.09072 12.08590 12.08124 12.07668 12.07220 12.06775 12.06331 12.05884
## [297] 12.05431 12.04967 12.04489 12.03994 12.03478 12.02938 12.02369 12.01770
## [305] 12.01135 12.00462 11.99747 11.98987 11.98053 11.96840 11.95377 11.93696
## [313] 11.91826 11.89796 11.87638 11.85381 11.83055 11.80690 11.78316 11.75963
## [321] 11.73662 11.71442 11.69333 11.67366 11.65570 11.63975 11.62612 11.61510
## [329] 11.60700 11.59943 11.58998 11.57889 11.56642 11.55281 11.53832 11.52319
## [337] 11.50768 11.49203 11.47650 11.46133 11.44678 11.43310 11.42053 11.40932
## [345] 11.39973 11.39201 11.38640 11.38315 11.38252 11.38476 11.38965 11.39670
## [353] 11.40577 11.41668 11.42929 11.44342 11.45891 11.47562 11.49336 11.51200
## [361] 11.53136 11.55128 11.57161 11.59218 11.61284 11.63342 11.65376 11.67370
## [369] 11.69309 11.71176 11.72954 11.74629 11.76426 11.78561 11.81000 11.83712
## [377] 11.86662 11.89818 11.93147 11.96616 12.00193 12.03844 12.07537 12.11239
## [385] 12.14916 12.18536 12.22067 12.25474 12.28726 12.31789 12.34631 12.37218
## [393] 12.39518 12.41498 12.43399 12.45467 12.47682 12.50022 12.52464 12.54987
## [401] 12.57569 12.60187 12.62821 12.65447 12.68045 12.70592 12.73066 12.75445
## [409] 12.77708 12.79833 12.81797 12.83578 12.85156 12.86507 12.87611 12.88532
## [417] 12.89353 12.90078 12.90710 12.91253 12.91709 12.92082 12.92376 12.92594
## [425] 12.92740 12.92816 12.92826 12.92773 12.92662 12.92494 12.92274 12.92006
## [433] 12.91691 12.91335 12.90939 12.90508 12.90045 12.89553 12.89036 12.88497
## [441] 12.87939 12.87183 12.86070 12.84636 12.82916 12.80948 12.78766 12.76408
## [449] 12.73907 12.71302 12.68627 12.65919 12.63214 12.60547 12.57954 12.55472
## [457] 12.53136 12.50983 12.49047 12.47366 12.45975 12.44910 12.43908 12.42696
## [465] 12.41300 12.39745 12.38055 12.36256 12.34372 12.32428 12.30450 12.28461
## [473] 12.26488 12.24554 12.22685 12.20905 12.19240 12.17715 12.16353 12.15181
## [481] 12.14223 12.13504 12.13048 12.12747 12.12474 12.12232 12.12022 12.11844
## [489] 12.11701 12.11594 12.11525 12.11494 12.11503 12.11554 12.11648 12.11787
## [497] 12.11971 12.12203 12.12483 12.12813 12.13195 12.13630 12.14119 12.14664
## [505] 12.15409 12.16478 12.17841 12.19470 12.21335 12.23407 12.25657 12.28057
## [513] 12.30576 12.33185 12.35856 12.38559 12.41266 12.43946 12.46572 12.49113
## [521] 12.51540 12.53825 12.55938 12.57851 12.59533 12.60956 12.62091 12.63187
## [529] 12.64502 12.66016 12.67707 12.69556 12.71544 12.73649 12.75851 12.78131
## [537] 12.80469 12.82844 12.85235 12.87624 12.89990 12.92313 12.94573 12.96749
## [545] 12.98821 13.00770 13.02575 13.04216 13.05673 13.06926 13.07955 13.08740
## [553] 13.09260 13.09495 13.09426 13.09070 13.08473 13.07653 13.06630 13.05424
## [561] 13.04054 13.02540 13.00901 12.99156 12.97326 12.95429 12.93485 12.91514
## [569] 12.89535 12.87567 12.85630 12.83744 12.81927 12.80201 12.78583 12.77093
## [577] 12.75470 12.73465 12.71116 12.68462 12.65542 12.62394 12.59057 12.55570
## [585] 12.51971 12.48299 12.44594 12.40892 12.37234 12.33658 12.30203 12.26907
## [593] 12.23808 12.20947 12.18361 12.16089 12.14169 12.12338 12.10315 12.08122
## [601] 12.05777 12.03300 12.00711 11.98028 11.95271 11.92459 11.89613 11.86751
## [609] 11.83893 11.81059 11.78267 11.75537 11.72889 11.70342 11.67915 11.65629
## [617] 11.63501 11.61553 11.59802 11.58270 11.56974 11.55935 11.55172 11.54639
## [625] 11.54271 11.54057 11.53987 11.54051 11.54239 11.54540 11.54945 11.55443
## [633] 11.56023 11.56677 11.57392 11.58160 11.58970 11.59812 11.60675 11.61550
## [641] 11.62425 11.63292 11.64140 11.64957 11.65922 11.67197 11.68753 11.70561
## [649] 11.72593 11.74820 11.77212 11.79741 11.82378 11.85094 11.87860 11.90647
## [657] 11.93426 11.96169 11.98846 12.01428 12.03887 12.06194 12.08320 12.10236
## [665] 12.11912 12.13530 12.15281 12.17152 12.19129 12.21200 12.23351 12.25569
## [673] 12.27842 12.30157 12.32499 12.34857 12.37217 12.39566 12.41891 12.44178
## [681] 12.46416 12.48591 12.50689 12.52698 12.54605 12.56396 12.58059 12.59580
## [689] 12.61017 12.62433 12.63828 12.65204 12.66558 12.67892 12.69204 12.70496
## [697] 12.71766 12.73014 12.74240 12.75445 12.76627 12.77787 12.78924 12.80039
## [705] 12.81130 12.82199 12.83244 12.84266 12.85263 12.86239 12.87192 12.88124
## [713] 12.89033 12.89921 12.90787 12.91631 12.92453 12.93253 12.94031 12.94786
## [721] 12.95519 12.96230 12.96919 12.97584 12.98228 12.98849 12.99447 13.00022
## [729] 13.00575 13.01105 13.01612 13.02096 13.02558 13.02998 13.03414 13.03809
## [737] 13.04180 13.04530 13.04857 13.05163 13.05446 13.05706 13.05945 13.06162
## [745] 13.06357 13.06530 13.06682 13.06812 13.06920 13.07006 13.07071 13.07115
## [753] 13.07137 13.07138 13.07117 13.07075 13.07011 13.06926 13.06819 13.06691
## [761] 13.06541 13.06369 13.06175 13.05960 13.05722 13.05463 13.05182 13.04879
## [769] 13.04553 13.04206 13.03836 13.03444
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 772)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.04815 12.04185 12.03563 12.02950 12.02346 12.01750 12.01162 12.00583
## [9] 12.00012 11.99449 11.98894 11.98347 11.97808 11.97276 11.96752 11.96236
## [17] 11.95727 11.95225 11.94730 11.94242 11.93762 11.93288 11.92821 11.92361
## [25] 11.91907 11.91460 11.91019 11.90584 11.90155 11.89733 11.89316 11.88906
## [33] 11.88501 11.88101 11.87708 11.87319 11.86936 11.86559 11.86186 11.85819
## [41] 11.85456 11.85098 11.84745 11.84397 11.84053 11.83713 11.83378 11.83047
## [49] 11.82720 11.82398 11.82078 11.81759 11.81443 11.81130 11.80820 11.80513
## [57] 11.80211 11.79913 11.79619 11.79332 11.79050 11.78774 11.78506 11.78244
## [65] 11.77990 11.77744 11.77507 11.77279 11.77060 11.76851 11.76653 11.76465
## [73] 11.76289 11.76124 11.75971 11.75831 11.75705 11.75591 11.75492 11.75407
## [81] 11.75337 11.75282 11.75243 11.75221 11.75215 11.75226 11.75254 11.75301
## [89] 11.75366 11.75450 11.75554 11.75677 11.75809 11.75939 11.76068 11.76197
## [97] 11.76327 11.76459 11.76594 11.76734 11.76880 11.77032 11.77192 11.77360
## [105] 11.77539 11.77728 11.77930 11.78145 11.78374 11.78619 11.78880 11.79159
## [113] 11.79457 11.79774 11.80112 11.80473 11.80856 11.81264 11.81697 11.82156
## [121] 11.82643 11.83158 11.83704 11.84280 11.84887 11.85528 11.86203 11.86914
## [129] 11.87753 11.88803 11.90042 11.91452 11.93013 11.94706 11.96510 11.98407
## [137] 12.00376 12.02399 12.04455 12.06524 12.08589 12.10627 12.12621 12.14551
## [145] 12.16396 12.18138 12.19756 12.21232 12.22545 12.23965 12.25747 12.27852
## [153] 12.30243 12.32881 12.35727 12.38742 12.41890 12.45129 12.48424 12.51734
## [161] 12.55021 12.58247 12.61374 12.64362 12.67174 12.69771 12.72115 12.74166
## [169] 12.75886 12.77238 12.78392 12.79542 12.80685 12.81817 12.82933 12.84032
## [177] 12.85108 12.86157 12.87177 12.88164 12.89113 12.90020 12.90883 12.91697
## [185] 12.92459 12.93164 12.93809 12.94391 12.94905 12.95348 12.95716 12.96005
## [193] 12.96211 12.96332 12.96362 12.96298 12.96137 12.95874 12.95507 12.95030
## [201] 12.94441 12.93735 12.92909 12.91960 12.90718 12.89050 12.86997 12.84605
## [209] 12.81915 12.78971 12.75817 12.72497 12.69053 12.65530 12.61971 12.58418
## [217] 12.54916 12.51509 12.48239 12.45150 12.42285 12.39689 12.37404 12.35473
## [225] 12.33606 12.31499 12.29177 12.26667 12.23993 12.21182 12.18258 12.15249
## [233] 12.12179 12.09075 12.05961 12.02864 11.99809 11.96822 11.93928 11.91153
## [241] 11.88524 11.86065 11.83802 11.81761 11.79967 11.78352 11.76824 11.75378
## [249] 11.74006 11.72704 11.71465 11.70284 11.69153 11.68067 11.67020 11.66006
## [257] 11.65018 11.64052 11.63099 11.62155 11.61214 11.60269 11.59314 11.58343
## [265] 11.57351 11.56330 11.55275 11.54181 11.53158 11.52311 11.51624 11.51080
## [273] 11.50663 11.50355 11.50140 11.50002 11.49923 11.49886 11.49876 11.49875
## [281] 11.49866 11.49834 11.49761 11.49630 11.49425 11.49129 11.48725 11.48197
## [289] 11.47528 11.46789 11.46063 11.45348 11.44643 11.43949 11.43264 11.42587
## [297] 11.41918 11.41256 11.40600 11.39949 11.39303 11.38661 11.38023 11.37386
## [305] 11.36752 11.36118 11.35485 11.34850 11.34215 11.33577 11.32786 11.31710
## [313] 11.30383 11.28835 11.27098 11.25205 11.23186 11.21074 11.18901 11.16698
## [321] 11.14497 11.12329 11.10227 11.08223 11.06348 11.04633 11.03111 11.01814
## [329] 11.00773 11.00019 10.99586 10.99321 10.99056 10.98798 10.98552 10.98324
## [337] 10.98120 10.97947 10.97811 10.97717 10.97671 10.97680 10.97750 10.97887
## [345] 10.98096 10.98384 10.98757 10.99221 10.99782 11.00446 11.01219 11.02107
## [353] 11.03184 11.04509 11.06063 11.07829 11.09791 11.11930 11.14229 11.16671
## [361] 11.19238 11.21913 11.24678 11.27516 11.30410 11.33342 11.36294 11.39250
## [369] 11.42191 11.45101 11.47962 11.50756 11.53467 11.56076 11.58566 11.60920
## [377] 11.63121 11.65150 11.67208 11.69489 11.71967 11.74617 11.77415 11.80335
## [385] 11.83351 11.86439 11.89572 11.92727 11.95877 11.98998 12.02064 12.05050
## [393] 12.07930 12.10680 12.13275 12.15688 12.17894 12.19870 12.21588 12.23196
## [401] 12.24851 12.26542 12.28261 12.29997 12.31740 12.33482 12.35212 12.36921
## [409] 12.38599 12.40237 12.41825 12.43352 12.44811 12.46190 12.47481 12.48673
## [417] 12.49757 12.50723 12.51562 12.52264 12.52820 12.53235 12.53517 12.53675
## [425] 12.53720 12.53660 12.53504 12.53263 12.52944 12.52558 12.52114 12.51621
## [433] 12.51088 12.50524 12.49939 12.49343 12.48744 12.48151 12.47575 12.47023
## [441] 12.46507 12.46034 12.45614 12.45123 12.44443 12.43591 12.42587 12.41447
## [449] 12.40191 12.38836 12.37400 12.35901 12.34358 12.32789 12.31211 12.29642
## [457] 12.28102 12.26608 12.25177 12.23829 12.22581 12.21451 12.20457 12.19618
## [465] 12.18766 12.17732 12.16539 12.15206 12.13755 12.12205 12.10578 12.08893
## [473] 12.07171 12.05433 12.03700 12.01992 12.00329 11.98732 11.97221 11.95818
## [481] 11.94542 11.93414 11.92455 11.91685 11.91125 11.90663 11.90177 11.89673
## [489] 11.89159 11.88641 11.88127 11.87623 11.87135 11.86672 11.86238 11.85842
## [497] 11.85490 11.85188 11.84945 11.84765 11.84657 11.84627 11.84681 11.84827
## [505] 11.85072 11.85421 11.85943 11.86689 11.87640 11.88779 11.90090 11.91554
## [513] 11.93154 11.94873 11.96694 11.98598 12.00569 12.02589 12.04640 12.06706
## [521] 12.08768 12.10810 12.12814 12.14762 12.16638 12.18423 12.20101 12.21653
## [529] 12.23063 12.24312 12.25385 12.26262 12.27108 12.28091 12.29197 12.30413
## [537] 12.31728 12.33127 12.34599 12.36130 12.37708 12.39320 12.40953 12.42595
## [545] 12.44232 12.45853 12.47444 12.48992 12.50485 12.51910 12.53254 12.54504
## [553] 12.55648 12.56673 12.57566 12.58315 12.58906 12.59328 12.59566 12.59609
## [561] 12.59476 12.59206 12.58809 12.58295 12.57675 12.56959 12.56160 12.55286
## [569] 12.54349 12.53360 12.52329 12.51268 12.50186 12.49094 12.48004 12.46926
## [577] 12.45870 12.44848 12.43869 12.42946 12.42088 12.41095 12.39781 12.38180
## [585] 12.36322 12.34242 12.31970 12.29540 12.26985 12.24336 12.21626 12.18888
## [593] 12.16154 12.13456 12.10827 12.08299 12.05906 12.03678 12.01650 11.99853
## [601] 11.98319 11.97082 11.95910 11.94568 11.93075 11.91453 11.89724 11.87908
## [609] 11.86027 11.84103 11.82156 11.80208 11.78280 11.76394 11.74571 11.72831
## [617] 11.71197 11.69689 11.68330 11.67139 11.66139 11.65351 11.64796 11.64451
## [625] 11.64270 11.64243 11.64355 11.64595 11.64951 11.65410 11.65959 11.66588
## [633] 11.67282 11.68031 11.68821 11.69640 11.70476 11.71317 11.72151 11.72964
## [641] 11.73745 11.74482 11.75162 11.75772 11.76443 11.77300 11.78326 11.79503
## [649] 11.80812 11.82236 11.83757 11.85357 11.87019 11.88723 11.90453 11.92191
## [657] 11.93918 11.95617 11.97269 11.98858 12.00364 12.01771 12.03060 12.04213
## [665] 12.05212 12.06170 12.07203 12.08305 12.09467 12.10680 12.11937 12.13230
## [673] 12.14550 12.15890 12.17241 12.18596 12.19945 12.21282 12.22598 12.23885
## [681] 12.25134 12.26338 12.27489 12.28578 12.29598 12.30540 12.31396 12.32159
## [689] 12.32849 12.33495 12.34101 12.34669 12.35202 12.35704 12.36178 12.36627
## [697] 12.37054 12.37461 12.37853 12.38231 12.38600 12.38962 12.39320 12.39678
## [705] 12.40039 12.40405 12.40780 12.41166 12.41567 12.41968 12.42349 12.42713
## [713] 12.43060 12.43389 12.43702 12.44000 12.44282 12.44549 12.44803 12.45043
## [721] 12.45270 12.45485 12.45687 12.45879 12.46060 12.46230 12.46391 12.46543
## [729] 12.46687 12.46822 12.46946 12.47056 12.47150 12.47231 12.47297 12.47350
## [737] 12.47389 12.47414 12.47427 12.47426 12.47413 12.47387 12.47349 12.47299
## [745] 12.47237 12.47164 12.47079 12.46984 12.46877 12.46760 12.46633 12.46496
## [753] 12.46351 12.46196 12.46033 12.45861 12.45679 12.45487 12.45285 12.45073
## [761] 12.44850 12.44617 12.44372 12.44117 12.43850 12.43571 12.43280 12.42978
## [769] 12.42662 12.42334 12.41994 12.41640
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")